import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

// 1. 先定义所有路由
const routes = [
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/index.vue'), // 确保路径正确
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  },
  // 其他路由...
]

// 2. 后创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 3. 最后添加导航守卫
router.beforeEach((to) => {
  const userStore = useUserStore()
  if (to.meta.requiresAuth && !userStore.isAuthenticated) {
    return '/login'
  }
})

export default router
